package com.shard.knabejay.birthday;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;

/**
 * Created by KnabeJay on 16/5/6.
 */
public class ListActivity extends Activity implements View.OnClickListener {
    private ArrayList<Person> personList;

    private static final String TAG = "ListActivity";
    private MyDatabaseHelper dbHelper;
    private Button btn_add;
    private RecyclerView mRecyclerView;
    private RecyclerView.LayoutManager mLayoutManager;
    private MyAdapter mAdapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list);

        btn_add = (Button) findViewById(R.id.btn_add);
        btn_add.setOnClickListener(this);
        personList = new ArrayList<>();

        dbHelper = new MyDatabaseHelper(this, "Birthday.db", null, 1);
        getData();

        mRecyclerView = (RecyclerView)findViewById(R.id.recycler_view);
        mLayoutManager = new LinearLayoutManager(getApplicationContext());
        mRecyclerView.setLayoutManager(mLayoutManager);
        //mRecyclerView.setHasFixedSize(true);
        mAdapter = new MyAdapter(getApplicationContext(),personList);
        mRecyclerView.setAdapter(mAdapter);
    }

    public void getData(){
        personList.clear();
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        Cursor cursor = db.query("birthday", null, null, null, null, null, null);
        if (cursor.moveToFirst()){
            do{
                Person person = new Person();
                person.setName(cursor.getString(cursor.getColumnIndex("name")));
                person.setDate(cursor.getString(cursor.getColumnIndex("date")));
                person.setPhone(cursor.getString(cursor.getColumnIndex("phone")));
                personList.add(person);
                Log.i(TAG, "getData: "+ person.getName()+person.getPhone());
            }while (cursor.moveToNext());
        }
        cursor.close();
    }

    @Override
    public void onClick(View v) {
        SQLiteDatabase db = dbHelper.getWritableDatabase();

        db.beginTransaction();//开始事务
        try {

            ContentValues values = new ContentValues();

            //开始插入数据
            values.put("name","ddd");
            values.put("date",1993-12-29);
            values.put("phone","1329847139");

            db.insert("birthday",null,values);

            values.put("name","小明");
            values.put("date",1995-12-29);
            values.put("phone","3334455");
            db.insert("birthday",null,values);

            values.put("name","小红");
            values.put("date",1895-12-29);
            values.put("phone","33344333335");
            db.insert("birthday",null,values);

            db.setTransactionSuccessful();//事务执行成功
            finish();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            db.endTransaction();//结束事务
        }

    }
}

